package com.atguigu.edu.realtime.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.edu.realtime.common.EduConfig;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.api.java.tuple.Tuple2;
import redis.clients.jedis.Jedis;

import java.util.List;

public class DimUtil {

    public static JSONObject getDimInfo(String tableName, String id){
        return getDimInfo(tableName, Tuple2.of("id",id));
    }


    public static JSONObject getDimInfo(String tableName, Tuple2<String, String>... columnNameAndValues) {
        //拼接从redis中查询数据的key
        StringBuilder redisKey = new StringBuilder("dim:" + tableName.toLowerCase() + ":");
        //拼接查询sql
        StringBuilder selectSql = new StringBuilder("select * from " + EduConfig.PHOENIX_SCHEMA + "." + tableName + " where ");
        for (int i = 0; i < columnNameAndValues.length; i++) {
            Tuple2<String, String> columnNameAndValue = columnNameAndValues[i];
            String columnName = columnNameAndValue.f0;
            String columnValue = columnNameAndValue.f1;
            selectSql.append(columnName + " ='"+columnValue+"' ");
            redisKey.append(columnValue);
            if (i< columnNameAndValues.length-1){
                selectSql.append(" and ");
                redisKey.append("_");
            }}
        // 操作 Redis 的客户端对象
        Jedis jedis = null;
        // 从 Redis 中查询的维度结果
        String dimJsonStr = null;
        // 方法的返回结果
        JSONObject dimJsonObj = null;

        try {
            jedis = JedisUtil.getJedis();
            // 从Redis中获取维度数据
            dimJsonStr = jedis.get(redisKey.toString());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("从Redis中查询维度数据发生了异常");
        }
        if(StringUtils.isNotEmpty(dimJsonStr)){
            dimJsonObj = JSON.parseObject(dimJsonStr);
        }else {
            System.out.println("从phoenix中查询的selectSql"+selectSql);
            //底层还是调用phoenixUtil方法
            List<JSONObject> resList = PhoenixUtil.queryList(selectSql.toString(), JSONObject.class);
            //JSONObject dimJsonObj = null;
            if (resList != null && resList.size() > 0) {

                dimJsonObj = resList.get(0);
                if (jedis!=null){
                    jedis.setex(redisKey.toString(),3600*24,dimJsonObj.toJSONString());
                }
            } else {
                System.out.println("没有在phoenix表中查询到数据");
            }
        }
        //释放资源
        if(jedis != null){
            System.out.println("关闭Jedis");
            jedis.close();
        }
        return dimJsonObj;
    }
    //代码复用性更强
    //查询维度的方法 没有进行优化
    public static JSONObject getDimInfoNoCache(String tableName, Tuple2<String, String>... columnNameAndValues) {
        //拼接查询sql
        StringBuilder selectSql = new StringBuilder("select * from " + EduConfig.PHOENIX_SCHEMA + "." + tableName + " where ");
        for (int i = 0; i < columnNameAndValues.length; i++) {
            Tuple2<String, String> columnNameAndValue = columnNameAndValues[i];
            String columnName = columnNameAndValue.f0;
            String columnValue = columnNameAndValue.f1;
            selectSql.append(columnName + " ='"+columnValue+"' ");
            if (i< columnNameAndValues.length-1){
                selectSql.append(" and ");
            }
        }
        System.out.println("从phoenix中查询的selectSql"+selectSql);
        //底层还是调用phoenixUtil方法
        List<JSONObject> resList = PhoenixUtil.queryList(selectSql.toString(), JSONObject.class);
        JSONObject dimJsonObj = null;
        if (resList != null && resList.size() > 0) {
            dimJsonObj = resList.get(0);
        } else {
            System.out.println("没有在phoenix表中查询到数据");
        }
        return dimJsonObj;
    }
    public static void delCached(String tableName,String id){
        String redisKey = "dim:"+tableName.toLowerCase()+":" + id;
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            jedis.del(redisKey);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null){
                System.out.println("删除维度后，关闭Jedis客户端");
                jedis.close();
            }
        }
    }

    public static void main(String[] args) {
        // System.out.println(getDimInfoNoCache("DIM_BASE_TRADEMARK",Tuple2.of("id","1"),Tuple2.of("tm_name","Redm")));
        // System.out.println(getDimInfo("DIM_BASE_TRADEMARK","1"));
        System.out.println(getDimInfo("DIM_BASE_TRADEMARK","1"));
    }
}
